<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Pisces","version":"7.7.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="基础语法variable “&#x3D;”左边的值赋值给右边的变量 变量名只能包含字母、数字和下划线，不能以数字打头，不能包含空格（下划线分隔单词），不要将 Python 关键字和函数名用作变量名，即不要使用 Python 保留用于特殊 用途的单词。  输入 help(“keywords”) 回车查看关键字 交互模式下输入 dir(builtins) 回车查看内置函数 多元复制  a,b,c &#x3D; a,2,”">
<meta property="og:type" content="article">
<meta property="og:title" content="课堂笔记">
<meta property="og:url" content="http://yoursite.com/2020/04/30/%E8%AF%BE%E5%A0%82%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="限定版小睿">
<meta property="og:description" content="基础语法variable “&#x3D;”左边的值赋值给右边的变量 变量名只能包含字母、数字和下划线，不能以数字打头，不能包含空格（下划线分隔单词），不要将 Python 关键字和函数名用作变量名，即不要使用 Python 保留用于特殊 用途的单词。  输入 help(“keywords”) 回车查看关键字 交互模式下输入 dir(builtins) 回车查看内置函数 多元复制  a,b,c &#x3D; a,2,”">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-04-30T08:46:57.000Z">
<meta property="article:modified_time" content="2020-04-30T08:47:58.291Z">
<meta property="article:author" content="樱桃小睿">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/04/30/%E8%AF%BE%E5%A0%82%E7%AC%94%E8%AE%B0/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true
  };
</script>

  <title>课堂笔记 | 限定版小睿</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">限定版小睿</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-right"></div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-schedule">

    <a href="/schedule/" rel="section"><i class="fa fa-fw fa-calendar"></i>日程表</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>站点地图</a>

  </li>
        <li class="menu-item menu-item-commonweal">

    <a href="/404/" rel="section"><i class="fa fa-fw fa-heartbeat"></i>公益 404</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/04/30/%E8%AF%BE%E5%A0%82%E7%AC%94%E8%AE%B0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/QQ.jpg">
      <meta itemprop="name" content="樱桃小睿">
      <meta itemprop="description" content="可甜可御pick me  up">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="限定版小睿">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          课堂笔记
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-04-30 16:46:57 / 修改时间：16:47:58" itemprop="dateCreated datePublished" datetime="2020-04-30T16:46:57+08:00">2020-04-30</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="基础语法"><a href="#基础语法" class="headerlink" title="基础语法"></a>基础语法</h1><h2 id="variable"><a href="#variable" class="headerlink" title="variable"></a>variable</h2><ul>
<li>“=”左边的值赋值给右边的变量</li>
<li>变量名只能包含字母、数字和下划线，不能以数字打头，不能包含空格（下划线分隔单词），不要将 Python 关键字和函数名用作变量名，即不要使用 Python 保留用于特殊 用途的单词。 </li>
<li>输入 help(“keywords”) 回车查看关键字</li>
<li>交互模式下输入 dir(<strong>builtins</strong>) 回车查看内置函数</li>
<li>多元复制  a,b,c = a,2,”abc”  此时等号两边的对象都是元组</li>
<li>交换变量值不需要中间变量  a,b = b,a</li>
<li>查看变量类型用type(变量名)</li>
</ul>
<h2 id="tuple"><a href="#tuple" class="headerlink" title="tuple"></a>tuple</h2><ul>
<li>元组使用小括号()  元组的函数无法修改，是不可变的数据类型，可以容纳不同的数据类型<br>可以通过索引访问函数、通过切片获取元素</li>
</ul>
<h4 id="切片"><a href="#切片" class="headerlink" title="切片"></a>切片</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">tup = (<span class="number">1</span>, <span class="number">20</span>, <span class="number">3</span>, <span class="number">40</span>, [<span class="number">5</span>, <span class="number">60</span>]) </span><br><span class="line">print(tup[<span class="number">1</span>:<span class="number">3</span>])</span><br><span class="line"></span><br><span class="line">- 如果元组中嵌套了列表，可以修改这个列表</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">tup = (<span class="number">1</span>, <span class="number">20</span>, <span class="number">3</span>, <span class="number">40</span>, [<span class="number">5</span>, <span class="number">60</span>])</span><br><span class="line">tup[<span class="number">4</span>][<span class="number">0</span>] = <span class="number">50</span> </span><br><span class="line">print(tup)</span><br></pre></td></tr></table></figure>

<h4 id="元组拆包"><a href="#元组拆包" class="headerlink" title="元组拆包"></a>元组拆包</h4><ul>
<li>取出元素<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a, b = (<span class="number">100</span>, <span class="number">55</span>) </span><br><span class="line">print(a,b)</span><br></pre></td></tr></table></figure></li>
<li>如果元组中存在不想要的元素，可使用占位符</li>
<li>如果元组中元素很多可以使用 * 表示任意多个元素（在元组长度范围之内）<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">tup &#x3D; (&quot;四川省&quot;, &quot;成都市&quot;, &quot;龙泉驿去&quot;, &quot;十陵街道&quot;, &quot;成都大学&quot;) </span><br><span class="line">*_, school &#x3D; tup </span><br><span class="line">print(school)</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h4 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h4><ul>
<li>如果一个元组对象中只有一个元素，那么这个元素后面一定要接一个 , （逗号）不然这个对像就不是元组类型</li>
</ul>
<h2 id="transform"><a href="#transform" class="headerlink" title="transform"></a>transform</h2><ul>
<li>很多时候我们需要对变量进行操作的时候，变量的类型不支持我们操作，例如：<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">str1 = <span class="string">"1958"</span> </span><br><span class="line">str2 = <span class="string">"4096"</span></span><br><span class="line">print(str1+str2)</span><br></pre></td></tr></table></figure></li>
<li>如果直接相加，那么得到的结果显然不是我们想要的，因为字符串相加是拼接操作，想要他们能进行数值上的相加， 就需要将他们的类型转换为数值的类型 </li>
</ul>
<h4 id="int"><a href="#int" class="headerlink" title="int"></a>int</h4><ul>
<li>支持转换为 int 类型的，仅有 float、str、bytes，其他类型均不支持。 </li>
</ul>
<ol>
<li>float -&gt; int 会去掉小数点及后面的数值，仅保留整数部分 </li>
<li>str -&gt; int 如果字符串中有数字(0-9)和正负号(+/-)以外的字符，就会报错。 </li>
<li>bytes -&gt; int 如果 bytes 中有数字(0-9)和正负号(+/-)以外的字符，就会报错。</li>
</ol>
<h4 id="float"><a href="#float" class="headerlink" title="float"></a>float</h4><ul>
<li>支持转换为 float 类型的，仅有 int、str、bytes，其他类型均不支持。与int类似 </li>
</ul>
<h4 id="str"><a href="#str" class="headerlink" title="str"></a>str</h4><ul>
<li>str() 函数可以将任意对象转换为字符串。</li>
</ul>
<ol>
<li>float -&gt; str 会将末位为0的部分除去</li>
<li>list -&gt; str</li>
</ol>
<h4 id="list"><a href="#list" class="headerlink" title="list"></a>list</h4><p>支持转换为 list 的类型，只能是序列，比如：str、tuple、dict、set等。 </p>
<ol>
<li>str -&gt; list</li>
<li>dict -&gt; list 字典转换为列表只取键</li>
</ol>
<ul>
<li>也可以使用 dict.values() 将值转放入列表 </li>
<li>如果使用 dict.items() ，那么就会将键和值成对处理成元组，然后放入列表  </li>
</ul>
<h2 id="string"><a href="#string" class="headerlink" title="string"></a>string</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">name = <span class="string">"tom"</span></span><br><span class="line">age = <span class="string">"18"</span></span><br><span class="line">job = <span class="string">"python工程师"</span></span><br><span class="line">print(<span class="string">"我是"</span> + <span class="string">"name"</span>，<span class="string">"我的年龄是"</span> + <span class="string">"age"</span>, <span class="string">"我的工作是"</span> + <span class="string">"job"</span>)</span><br><span class="line">print(<span class="string">"我是%s，我的年龄是%s，我的工作是%s"</span>%(name,age,job))</span><br></pre></td></tr></table></figure>
<h4 id="转义字符"><a href="#转义字符" class="headerlink" title="转义字符"></a>转义字符</h4><h4 id="三重引号"><a href="#三重引号" class="headerlink" title="三重引号"></a>三重引号</h4><ul>
<li>允许用 ‘’’…’’’ 或 “””…””” 的格 式表示多行内容</li>
</ul>
<h4 id="获取字符长度"><a href="#获取字符长度" class="headerlink" title="获取字符长度"></a>获取字符长度</h4><ul>
<li>len() 函数获取字符串的长度，len()函数我们会经常使用。<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">string = <span class="string">"hello python"</span> </span><br><span class="line">print(string) </span><br><span class="line">print(<span class="string">"字符串的长度是"</span>, len(string))</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h4 id="strip-删除开头或结尾空白字符"><a href="#strip-删除开头或结尾空白字符" class="headerlink" title="strip()删除开头或结尾空白字符"></a>strip()删除开头或结尾空白字符</h4><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">string = <span class="string">'helllo'</span>  </span><br><span class="line">print(string.strip())</span><br></pre></td></tr></table></figure>
<ul>
<li>如果字符串的开头和结尾都有空白字符，那么都会删除</li>
</ul>
<h4 id="count-获取某个字符在字符串出现的次数"><a href="#count-获取某个字符在字符串出现的次数" class="headerlink" title="count()获取某个字符在字符串出现的次数"></a>count()获取某个字符在字符串出现的次数</h4><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">string = <span class="string">"hello python"</span> </span><br><span class="line">print(<span class="string">"字符o的次数是："</span>,string.count(<span class="string">'o'</span>))</span><br></pre></td></tr></table></figure>

<h4 id="replace-oldstr-newstr-字符串替换"><a href="#replace-oldstr-newstr-字符串替换" class="headerlink" title="replace(oldstr,newstr)字符串替换"></a>replace(oldstr,newstr)字符串替换</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">string &#x3D; &quot;hello python&quot; </span><br><span class="line">new_str &#x3D; string.replace(&#39;h&#39;,&quot;H&quot;) </span><br><span class="line">print(new_str)</span><br></pre></td></tr></table></figure>

<h4 id="format-字符串格式化"><a href="#format-字符串格式化" class="headerlink" title="format()字符串格式化"></a>format()字符串格式化</h4><ul>
<li>除了使用 % ，还可以使用format对字符串进行格式化<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">string = <span class="string">'hello&#123;&#125;'</span> </span><br><span class="line">new_str = string.format(<span class="string">'你好'</span>) </span><br><span class="line">print(new_str)</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h4 id="split-字符串的分割"><a href="#split-字符串的分割" class="headerlink" title="split()字符串的分割"></a>split()字符串的分割</h4><p>如果括号中不添加参数，那么默认以空格作为分隔符<br>注意：分割字符串返回是一个列表，列表里面就是分割的字符串</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">string = <span class="string">"hello world"</span> </span><br><span class="line">print(string.split(<span class="string">" "</span>))</span><br></pre></td></tr></table></figure>

<h4 id="join-连接字符串"><a href="#join-连接字符串" class="headerlink" title="join()连接字符串"></a>join()连接字符串</h4><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">string = <span class="string">"hello"</span> </span><br><span class="line">new_str = <span class="string">'你好'</span>.join(string) </span><br><span class="line">print(new_str)</span><br></pre></td></tr></table></figure>

<h4 id="字符串相加，字符串相乘"><a href="#字符串相加，字符串相乘" class="headerlink" title="字符串相加，字符串相乘"></a>字符串相加，字符串相乘</h4><p>可以发现：字符串和字符串相加只是拼接字符串，而字符串和数字相乘则是复制三个相同的字符串。</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">str1 = <span class="string">"hello"</span> </span><br><span class="line">str2 = <span class="string">"world"</span> </span><br><span class="line">new_str1 = str1 + str2 </span><br><span class="line">new_str2 = str1 * <span class="number">3</span> </span><br><span class="line">print(new_str1) </span><br><span class="line">print(new_str2)</span><br></pre></td></tr></table></figure>

<h4 id="字符串下标"><a href="#字符串下标" class="headerlink" title="字符串下标"></a>字符串下标</h4><ul>
<li>想要取出字符串中特定位置的字符，可以使用字符串的下标<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">string = <span class="string">"abc1234567"</span> </span><br><span class="line">new_str = string[<span class="number">2</span>] </span><br><span class="line">print(new_str)</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h4 id="字符串切片"><a href="#字符串切片" class="headerlink" title="字符串切片"></a>字符串切片</h4><ul>
<li>从指定的开始索引到指定的结束索引，即：<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">[start:end:step]</span><br><span class="line">string = <span class="string">"abc1234567"</span> </span><br><span class="line">print(string[<span class="number">2</span>:<span class="number">5</span>]) </span><br><span class="line">print(string[<span class="number">2</span>:]) </span><br><span class="line">print(string[<span class="number">2</span>::<span class="number">2</span>]) </span><br><span class="line">print(string[::<span class="number">2</span>]) </span><br><span class="line">print(string[::<span class="number">-1</span>]) </span><br><span class="line">print(string[<span class="number">-3</span>:<span class="number">-1</span>])</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h2 id="set"><a href="#set" class="headerlink" title="set"></a>set</h2><ul>
<li>Python中的集合和数学上的集合是一样的，可以进行交、并、差等运算,集合中不允许有重复元素,集合是无序的,集合内的元素是不可变<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">s1 = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125; </span><br><span class="line">print(s1)</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h4 id="集合运算"><a href="#集合运算" class="headerlink" title="集合运算"></a>集合运算</h4><ul>
<li>交集 s1 &amp;s2</li>
<li>并集 s1 | s2</li>
<li>补集 s1 - s2 (属于前者但不属于后者)</li>
</ul>
<h5 id="添加元素"><a href="#添加元素" class="headerlink" title="添加元素"></a>添加元素</h5><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">s = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125; </span><br><span class="line">s.add(<span class="number">4</span>) </span><br><span class="line">print(s)</span><br></pre></td></tr></table></figure>

<h5 id="移除指定元素"><a href="#移除指定元素" class="headerlink" title="移除指定元素"></a>移除指定元素</h5><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">s = &#123;<span class="number">11</span>, <span class="number">22</span>, <span class="number">33</span>, <span class="number">44</span>, <span class="number">55</span>&#125; </span><br><span class="line">s.remove(<span class="number">11</span>) </span><br><span class="line">print(s)</span><br></pre></td></tr></table></figure>

<h5 id="pop"><a href="#pop" class="headerlink" title="pop"></a>pop</h5><ul>
<li>可以删除列表指定位置的元素，并返回指定位置元素的值，不指定位置则删除末尾元素</li>
</ul>
<h5 id="difference"><a href="#difference" class="headerlink" title="difference"></a>difference</h5><ul>
<li>虽然也是返回补集，但是和 - 的功能相反</li>
</ul>
<h5 id="difference-update"><a href="#difference-update" class="headerlink" title="difference_update"></a>difference_update</h5><ul>
<li>移除两个集合中都存在的元素<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">s1 = &#123;<span class="number">11</span>, <span class="number">22</span>, <span class="number">33</span>, <span class="number">44</span>&#125; </span><br><span class="line">s2 = &#123;<span class="number">55</span>, <span class="number">66</span>, <span class="number">77</span>, <span class="number">22</span>, <span class="number">44</span>, <span class="number">88</span>&#125; </span><br><span class="line">s1.difference_update(s2) </span><br><span class="line">print(s1) </span><br><span class="line">print(s2)</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h5 id="in-not-in"><a href="#in-not-in" class="headerlink" title="in/not in"></a>in/not in</h5><ul>
<li>判断元素是否在集合内<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">s = &#123;<span class="number">11</span>, <span class="number">22</span>, <span class="number">33</span>, <span class="number">44</span>&#125; </span><br><span class="line">print(<span class="number">11</span> <span class="keyword">in</span> s) </span><br><span class="line">print(<span class="number">22</span> <span class="keyword">not</span> <span class="keyword">in</span> s)</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h5 id="去重"><a href="#去重" class="headerlink" title="去重"></a>去重</h5><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">squ = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">10</span>, <span class="number">3</span>, <span class="number">100</span>, <span class="number">2</span>, <span class="number">8</span>] squ = set(squ) </span><br><span class="line">squ = list(squ) </span><br><span class="line">print(squ)</span><br></pre></td></tr></table></figure>
<h2 id="列表"><a href="#列表" class="headerlink" title="列表"></a>列表</h2><ul>
<li>列表是Python中最常用的数据类型 </li>
<li>列表中可以容纳不同的数据类型 </li>
<li>列表是一种可变序列 </li>
<li>列表可以通过索引访问元素（和字符串一样索引是从0开始） </li>
<li>列表支持切片运算</li>
</ul>
<h4 id="创建列表"><a href="#创建列表" class="headerlink" title="创建列表"></a>创建列表</h4><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">· 创建空列表的两种方法 </span><br><span class="line">list_1 = [] </span><br><span class="line">list_2 = list()</span><br><span class="line">· 创建含有元素的空列表</span><br><span class="line">list_3 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="string">"a"</span>]</span><br></pre></td></tr></table></figure>

<h4 id="乘法"><a href="#乘法" class="headerlink" title="乘法"></a>乘法</h4><p>· 列表元素的重复</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">squ = [<span class="string">"python"</span>] * <span class="number">3</span> </span><br><span class="line">print(squ) </span><br><span class="line">输出为 [<span class="string">"python"</span>, <span class="string">"python"</span>, <span class="string">"python"</span>]</span><br></pre></td></tr></table></figure>

<h4 id="加法"><a href="#加法" class="headerlink" title="加法"></a>加法</h4><p>· 将列表拼接</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">squ1 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>] </span><br><span class="line">squ2 = [<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>] </span><br><span class="line">squ = squ1 + squ2 </span><br><span class="line">print(squ)</span><br></pre></td></tr></table></figure>

<h4 id="索引"><a href="#索引" class="headerlink" title="索引"></a>索引</h4><p>· 可以通过索引来改变列表元素的值，也可以通过列表来访问元素</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">squ = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="string">"A"</span>] </span><br><span class="line">print(squ[<span class="number">3</span>]) </span><br><span class="line">squ[<span class="number">3</span>] = <span class="number">4</span> </span><br><span class="line">print(squ)</span><br></pre></td></tr></table></figure>

<h4 id="切片-1"><a href="#切片-1" class="headerlink" title="切片"></a>切片</h4><p>  [start:end:step] </p>
<ul>
<li>start 起始位置 end 结束位置 step 步长, 默认是 1。 step 为正数时，从左到右；负数时，从右到左<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">list1 = [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>] </span><br><span class="line">print(list1[:<span class="number">5</span>]) <span class="comment"># 切片反转元素 </span></span><br><span class="line">print(list1[::<span class="number">-1</span>]</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h4 id="append"><a href="#append" class="headerlink" title="append"></a>append</h4><ul>
<li>向列表末尾添加元素<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">squ = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>] </span><br><span class="line">squ.append(<span class="string">"A"</span>) </span><br><span class="line">print(squ)</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h4 id="extend"><a href="#extend" class="headerlink" title="extend"></a>extend</h4><ul>
<li>一次性向列表末尾添加多个元素，对象可以直接为列表</li>
</ul>
<h4 id="insert"><a href="#insert" class="headerlink" title="insert"></a>insert</h4><ul>
<li>以将元素插入到列表的指定位置</li>
</ul>
<h4 id="remove"><a href="#remove" class="headerlink" title="remove"></a>remove</h4><ul>
<li>按值删除元素，找到第一个符合的元素然后删除</li>
</ul>
<h4 id="del"><a href="#del" class="headerlink" title="del"></a>del</h4><ul>
<li>删除指定列表中的元素</li>
</ul>
<h4 id="max-min"><a href="#max-min" class="headerlink" title="max/min"></a>max/min</h4><ul>
<li>返回列表中最大/小值</li>
</ul>
<h4 id="嵌套列表"><a href="#嵌套列表" class="headerlink" title="嵌套列表"></a>嵌套列表</h4><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">squ = [<span class="number">1</span>, <span class="number">2</span>, [<span class="number">3</span>, <span class="number">4</span>]] </span><br><span class="line">print(squ[<span class="number">2</span>][<span class="number">0</span>])</span><br></pre></td></tr></table></figure>

<h2 id="在Python中一般不用列表来表示二维数组或者多维数组，虽然Python也能表示，但是性能较低，一般使用array或者-numpy来创建多位数组"><a href="#在Python中一般不用列表来表示二维数组或者多维数组，虽然Python也能表示，但是性能较低，一般使用array或者-numpy来创建多位数组" class="headerlink" title="在Python中一般不用列表来表示二维数组或者多维数组，虽然Python也能表示，但是性能较低，一般使用array或者 numpy来创建多位数组"></a>在Python中一般不用列表来表示二维数组或者多维数组，虽然Python也能表示，但是性能较低，一般使用array或者 numpy来创建多位数组</h2><h2 id="整型（int）"><a href="#整型（int）" class="headerlink" title="整型（int）"></a>整型（int）</h2><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="string">'3'</span> </span><br><span class="line">b = <span class="string">'2'</span> </span><br><span class="line">c = a + b </span><br><span class="line">print(a + b) </span><br><span class="line">print(c)</span><br><span class="line"><span class="number">32</span></span><br><span class="line"><span class="number">32</span></span><br></pre></td></tr></table></figure>
<p>现在 a 、 b 变量已经不是一个 int 变量，而是字符串 (str) 变量</p>
<pre><code>+  两个对象相加
-  减 - 得到负数或是一个数减去另一个数 
*  乘 - 两个数相乘或是返回一个被重复若干次的字符串 
/  除 - x 除以 y 
%  取模 - 返回除法的余数 
** 幂 - 返回x的y次幂 
// 取整除 - 向下取接近除数的整数</code></pre><h3 id="整型不能用-len-函数"><a href="#整型不能用-len-函数" class="headerlink" title="整型不能用 len() 函数"></a>整型不能用 len() 函数</h3><h2 id="if"><a href="#if" class="headerlink" title="if"></a>if</h2><h3 id="if-else-语句"><a href="#if-else-语句" class="headerlink" title="if else 语句"></a>if else 语句</h3><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> 条件表达式:    </span><br><span class="line">    程序块<span class="number">1</span> </span><br><span class="line"><span class="keyword">else</span>:    </span><br><span class="line">    程序块<span class="number">2</span></span><br></pre></td></tr></table></figure>

<h3 id="if-elif-else"><a href="#if-elif-else" class="headerlink" title="if elif else"></a>if elif else</h3><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> 条件表达式<span class="number">1</span>:    </span><br><span class="line">    程序块<span class="number">1</span> </span><br><span class="line"><span class="keyword">elif</span> 条件表达式<span class="number">2</span>:    </span><br><span class="line">    程序块<span class="number">2</span> </span><br><span class="line"><span class="keyword">elif</span> ...:    </span><br><span class="line">    ..... </span><br><span class="line"><span class="keyword">else</span>:    </span><br><span class="line">    程序块</span><br></pre></td></tr></table></figure>

<h3 id="pass"><a href="#pass" class="headerlink" title="pass"></a>pass</h3><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">age = <span class="number">18</span> </span><br><span class="line"><span class="keyword">if</span> age &gt; <span class="number">18</span>:    </span><br><span class="line">    <span class="keyword">pass</span></span><br></pre></td></tr></table></figure>
<ul>
<li>pass 不做任何事情，一般用做占位语句。 </li>
<li>实际开发中，如果我们暂时没有想到逻辑，可以使用pass进行占位。</li>
</ul>
<h2 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h2><ul>
<li>函数代码块以 def 开头，后接函数标名和括号 () 最后就是一个英文冒号 : </li>
<li>传递的参数必须放在括号里面，圆括号内定义参数 </li>
<li>函数内容必须缩进 </li>
<li>函数的第一行使用文档字符串形式存放函数说明<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">print_hello</span><span class="params">()</span>:</span>   </span><br><span class="line">     <span class="string">"""重复打印hello"""</span>    </span><br><span class="line">     <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">5</span>):        </span><br><span class="line">         print(<span class="string">"hello"</span>)</span><br></pre></td></tr></table></figure>


</li>
</ul>
<h5 id="caution：函数没有调用的时候，这段代码不会执行"><a href="#caution：函数没有调用的时候，这段代码不会执行" class="headerlink" title="caution：函数没有调用的时候，这段代码不会执行"></a>caution：函数没有调用的时候，这段代码不会执行</h5><h4 id="函数参数"><a href="#函数参数" class="headerlink" title="函数参数"></a>函数参数</h4><h5 id="必备参数"><a href="#必备参数" class="headerlink" title="必备参数"></a>必备参数</h5><ul>
<li>必备参数必须以 正确的顺序传入函数。调用时的数量必须和声明时的一样。</li>
</ul>
<h5 id="默认参数"><a href="#默认参数" class="headerlink" title="默认参数"></a>默认参数</h5><ul>
<li>调用函数的时候，默认参数的值如果没有传入，则被认为是默认值。默认参数必须在参数的最后。</li>
</ul>
<h5 id="不定长参数"><a href="#不定长参数" class="headerlink" title="不定长参数"></a>不定长参数</h5><ul>
<li>如果处理一个函数比声明时需要更多的参数。那么可以使用不定长参数。</li>
</ul>
<h4 id="匿名参数"><a href="#匿名参数" class="headerlink" title="匿名参数"></a>匿名参数</h4><p>Python中用lambda来创建匿名参数</p>
<ul>
<li>lambda只是一个表达式，函数体简单 </li>
<li>lambda是一个表达式，而不是一个代码块，仅仅能在lambda表达式中封装有限的逻辑进去。 </li>
<li>lambda函数拥有自己的命名空间，且不能访问自有参数列表之外或全局命名空间里的参数。 </li>
<li>lambda可以接受任意数量的参数，但只能包含一个表达式<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">result = <span class="keyword">lambda</span> arg1, arg2 : arg1 + arg2</span><br><span class="line">print(result(<span class="number">10</span>, <span class="number">20</span>)</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h4 id="return"><a href="#return" class="headerlink" title="return"></a>return</h4><p>用于函数返回值，如果没有返回值就返回None</p>
<h4 id="全局变量和局部变量"><a href="#全局变量和局部变量" class="headerlink" title="全局变量和局部变量"></a>全局变量和局部变量</h4><ul>
<li>定义在函数内部的变量拥有一个局部作用域，定义在函数外拥有全局作用域。函数会优先使用局部变量，局部没有变 量则去找全部变量。<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">total = <span class="number">10</span> <span class="comment"># 全局变量</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">result</span><span class="params">(arg1, arg2)</span>:</span>    <span class="comment"># 局部变量total    </span></span><br><span class="line">    total = arg1 + arg2    </span><br><span class="line">    print(<span class="string">"函数内的局部变量是"</span>, total)     </span><br><span class="line">    <span class="keyword">return</span> total</span><br><span class="line">result(<span class="number">10</span>, <span class="number">20</span>) </span><br><span class="line">print(<span class="string">"函数外是全局变量"</span>, total</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h4 id="global"><a href="#global" class="headerlink" title="global"></a>global</h4><ul>
<li>将变量定义为全局变量，实现在函数内部修改变量值。 </li>
<li>一个global关键字可以同时定义多个变量，如global x,y,z <figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">num = <span class="number">10</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">set_num_value</span><span class="params">()</span>:</span>    <span class="comment"># 使用global声明全局变量    </span></span><br><span class="line">    <span class="keyword">global</span> num    </span><br><span class="line">    num = <span class="number">30</span>    </span><br><span class="line">    <span class="keyword">return</span> num</span><br><span class="line">print(set_num_value()) </span><br><span class="line">print(num)</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h4 id="递归函数"><a href="#递归函数" class="headerlink" title="递归函数"></a>递归函数</h4><ul>
<li>一个函数的内部调用了自己本身，那么这个函数就是递归函数。 <figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">sum_num</span><span class="params">(n)</span>:</span>    </span><br><span class="line">    <span class="keyword">if</span> n &lt;= <span class="number">0</span>:        </span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>    </span><br><span class="line">        <span class="keyword">return</span> n + sum_num(n<span class="number">-1</span>)</span><br><span class="line">print(sum_num(<span class="number">100</span>))</span><br><span class="line"><span class="number">5050</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
<h2 id="浮点数-float"><a href="#浮点数-float" class="headerlink" title="浮点数(float)"></a>浮点数(float)</h2><ul>
<li>之所以称为浮点数，是因为按照科学记数法表示时，一个浮点数的小数点位置是可变的</li>
<li>浮点 数运算则可能会有四舍五入的误差</li>
</ul>
<h3 id="文件操作"><a href="#文件操作" class="headerlink" title="文件操作"></a>文件操作</h3><h5 id="open函数负责打开文件，并且返回文件对象"><a href="#open函数负责打开文件，并且返回文件对象" class="headerlink" title="open函数负责打开文件，并且返回文件对象"></a>open函数负责打开文件，并且返回文件对象</h5><h5 id="read-write-close三个方法都需要调用文件对象来调用"><a href="#read-write-close三个方法都需要调用文件对象来调用" class="headerlink" title="read/write/close三个方法都需要调用文件对象来调用"></a>read/write/close三个方法都需要调用文件对象来调用</h5><h4 id="read方法读取文件"><a href="#read方法读取文件" class="headerlink" title="read方法读取文件"></a>read方法读取文件</h4><ul>
<li>open函数的第一个参数是要打开的文件名(文件名区分大小写) </li>
<li>如果文件存在，返回文件操作对象;文件不存在，抛出异常 </li>
<li>read方法可以一次性读入并且返回文件的所有内容 </li>
<li>close方法负责关闭文件<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 打开文件 </span></span><br><span class="line">file = open(<span class="string">"admin.txt"</span>)</span><br><span class="line"><span class="comment"># 读取文件内容 </span></span><br><span class="line">text = file.read() print(text)</span><br><span class="line"><span class="comment"># 关闭文件 </span></span><br><span class="line">file.close()</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h5 id="打开文件的方式"><a href="#打开文件的方式" class="headerlink" title="打开文件的方式"></a>打开文件的方式</h5><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">f = open(<span class="string">"文件名"</span>，<span class="string">"访问方式"</span>)</span><br></pre></td></tr></table></figure>
<ul>
<li>r 只读的方式打开文件， 默认就是只读 </li>
<li>w 只写的方式打开文件，如果文件存在则进行覆盖。如果不存在，创建新文件 </li>
<li>a 以追加的方式打开文件，如果文件存在，则将内容追加到末尾。如果文件不存在，则创建文件并写入 </li>
<li>r+ 读写的方式打开文件，文件不存在，抛出异常 </li>
<li>w+ 读写的方式打开文件，如果文件存在直接覆盖，文件不存在，创建新文件 </li>
<li>a+ 读写的方式打开文件，如果文件不存在，创建新文件进行写入，文件存在，直接追加<h4 id="执行读取文件内容"><a href="#执行读取文件内容" class="headerlink" title="执行读取文件内容"></a>执行读取文件内容</h4><h5 id="read方法默认把文件的所有内容一次性读取到内存中，如果文件过大，内存占用会非常严重"><a href="#read方法默认把文件的所有内容一次性读取到内存中，如果文件过大，内存占用会非常严重" class="headerlink" title="read方法默认把文件的所有内容一次性读取到内存中，如果文件过大，内存占用会非常严重"></a>read方法默认把文件的所有内容一次性读取到内存中，如果文件过大，内存占用会非常严重</h5><h5 id="readline方法可以一次读取一行内容-方法执行之后，会把文件指针移动到下一行，准备再次读取"><a href="#readline方法可以一次读取一行内容-方法执行之后，会把文件指针移动到下一行，准备再次读取" class="headerlink" title="readline方法可以一次读取一行内容 方法执行之后，会把文件指针移动到下一行，准备再次读取"></a>readline方法可以一次读取一行内容 方法执行之后，会把文件指针移动到下一行，准备再次读取</h5><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">file = open(<span class="string">"file.txt"</span>) </span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:    </span><br><span class="line">    text = file.readline()    <span class="comment"># 判断是否有内容    </span></span><br><span class="line">    <span class="keyword">if</span> <span class="keyword">not</span> text:        </span><br><span class="line">        <span class="keyword">break</span>    </span><br><span class="line">print(text) </span><br><span class="line">file.close()</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="文件读写复制"><a href="#文件读写复制" class="headerlink" title="文件读写复制"></a>文件读写复制</h3><h4 id="小文件"><a href="#小文件" class="headerlink" title="小文件"></a>小文件</h4><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">file_read = open(<span class="string">"1.txt"</span>) </span><br><span class="line">file_write = open(<span class="string">"1附件.txt"</span>) </span><br><span class="line">text = file_read.read() </span><br><span class="line">file_write.write(text) </span><br><span class="line">file_read.close() </span><br><span class="line">file_write.close()</span><br></pre></td></tr></table></figure>

<h4 id="大文件"><a href="#大文件" class="headerlink" title="大文件"></a>大文件</h4><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">file_read = open(<span class="string">"1.txt"</span>) </span><br><span class="line">file_write = open(<span class="string">"1附件.txt"</span>) </span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:    </span><br><span class="line">    text = file_read.readline()        </span><br><span class="line">    <span class="keyword">if</span> <span class="keyword">not</span> text:        </span><br><span class="line">        <span class="keyword">break</span>            </span><br><span class="line">file_write.write(text)    </span><br><span class="line">file_read.close() </span><br><span class="line">file_write.close()</span><br></pre></td></tr></table></figure>

<h3 id="with-open"><a href="#with-open" class="headerlink" title="with open"></a>with open</h3><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">with</span> open(<span class="string">"1.txt"</span>) <span class="keyword">as</span> f:   </span><br><span class="line">    f.read()</span><br></pre></td></tr></table></figure>

<h2 id="error"><a href="#error" class="headerlink" title="error"></a>error</h2><h4 id="异常的捕获"><a href="#异常的捕获" class="headerlink" title="异常的捕获"></a>异常的捕获</h4><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    尝试执行的代码块</span><br><span class="line"><span class="keyword">except</span>：</span><br><span class="line">    处理异常</span><br></pre></td></tr></table></figure>

<h4 id="异常响应"><a href="#异常响应" class="headerlink" title="异常响应"></a>异常响应</h4><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span>:    </span><br><span class="line">    尝试运行的代码 </span><br><span class="line"><span class="keyword">except</span> 错误类型<span class="number">1</span>:    </span><br><span class="line">        针对错误类型<span class="number">1</span>进行对应代码处理 </span><br><span class="line"><span class="keyword">except</span> 错误类型<span class="number">2</span>:    </span><br><span class="line">            针对错误类型<span class="number">2</span>进行对应代码处理</span><br><span class="line"><span class="keyword">except</span> Exception <span class="keyword">as</span> e:    </span><br><span class="line">    print(<span class="string">"未知错误%s"</span> % e) <span class="keyword">else</span>:    没有异常时执行的代码 </span><br><span class="line"><span class="keyword">finally</span>:    无论是否有异常都会执行的代</span><br></pre></td></tr></table></figure>

<h4 id="异常的传递"><a href="#异常的传递" class="headerlink" title="异常的传递"></a>异常的传递</h4><ul>
<li>将异常传递给主程序，主程序如果没有处理异常，则程序终止。<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">demo1</span><span class="params">()</span>:</span>    </span><br><span class="line">    <span class="keyword">return</span> int(input(<span class="string">"请输入一个整数:"</span>)) </span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">demo2</span><span class="params">()</span>:</span>    </span><br><span class="line">    <span class="keyword">return</span> demo1() </span><br><span class="line">    <span class="keyword">try</span>:        </span><br><span class="line">        print(demo2())</span><br><span class="line">    <span class="keyword">except</span> valueErroe:    </span><br><span class="line">        print(<span class="string">"请输入正确的整数"</span>) </span><br><span class="line">    <span class="keyword">except</span> Exception <span class="keyword">as</span> e:    </span><br><span class="line">        print(<span class="string">"未知错误%s"</span> % e)</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h4 id="抛出异常"><a href="#抛出异常" class="headerlink" title="抛出异常"></a>抛出异常</h4><h5 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h5><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">input_pwd</span><span class="params">()</span>:</span>    </span><br><span class="line">    pwd = input(<span class="string">"请输入密码:"</span>)    </span><br><span class="line">    <span class="keyword">if</span> len(pwd) &gt;= <span class="number">8</span>:        </span><br><span class="line">        <span class="keyword">return</span> pwd   </span><br><span class="line">         exc = Exception(<span class="string">"密码长度不够"</span>)    <span class="keyword">raise</span> exc</span><br><span class="line"><span class="keyword">try</span>:    </span><br><span class="line">    user_pwd = input_pwd()    </span><br><span class="line">    print(user_pwd) <span class="keyword">except</span> </span><br><span class="line">    Exception <span class="keyword">as</span> e:    </span><br><span class="line">    print(<span class="string">"发现错误%s"</span> % e</span><br></pre></td></tr></table></figure>

<h2 id="字典"><a href="#字典" class="headerlink" title="字典"></a>字典</h2><ul>
<li>字典由键 key 和值 value 组成，使用 : 链接键值对</li>
<li>Python字典是可变类型数据，可以存储任意对象，如字符串，数字，元组，列表等。 </li>
<li>Python字典中的键是唯一的，且不可变的，必须是不可变类型<h3 id="删除"><a href="#删除" class="headerlink" title="删除"></a>删除</h3></li>
<li>pop(key) 删除指定键是key的条目 </li>
<li>del 删除整个字典对象 </li>
<li>clear 清空字典 </li>
<li>del dict[“key”] 删除指定键是key的条目</li>
</ul>
<h3 id="get"><a href="#get" class="headerlink" title="get"></a>get</h3><ul>
<li>获取键对应的值，没有这个键则返回传递的值<h3 id="update"><a href="#update" class="headerlink" title="update"></a>update</h3></li>
<li>连接两个字典</li>
</ul>
<h3 id="cycle"><a href="#cycle" class="headerlink" title="cycle"></a>cycle</h3><ul>
<li>跳过此次循环，进入下一次循环</li>
</ul>
<h1 id="正则表达式"><a href="#正则表达式" class="headerlink" title="正则表达式"></a>正则表达式</h1><h2 id="常见匹配模式"><a href="#常见匹配模式" class="headerlink" title="常见匹配模式"></a>常见匹配模式</h2><p><code>^</code> ：^会匹配行或者字符串的起始位置，有时还会匹配整个文档的起始位置。 </p>
<p><code>$</code>  ：$会匹配行或字符串的结尾</p>
<p><code>\b</code> :不会消耗任何字符只匹配一个位置，常用于匹配单词边界 如 我想从字符串中”This is Regex”匹配单独的单词 “is” 正则就要写成 “\bis\b”  </p>
<p>　　  \b 不会匹配is 两边的字符，但它会识别is 两边是否为单词的边界 </p>
<p><code>\d</code>: 匹配数字，例如要匹配一个固定格式的电话号码以0开头前4位后7位，如0737-5686123  正则:^0\d\d\d-\d\d\d\d\d\d\d$ </p>
<p><code>\w</code>：匹配字母，数字，下划线.</p>
<p>　　  例如我要匹配”a2345BCD__TTz” 正则：”\w+”  这里的”+”字符为一个量词指重复的次数，稍后会详细介绍。</p>
<p><code>\s</code>：匹配空格 </p>
<p>　　  例如字符 “a b c” 正则：”\w\s\w\s\w”  一个字符后跟一个空格，如有字符间有多个空格直接把”\s” 写成 “\s+” 让空格重复</p>
<p><code>.</code>：匹配除了换行符以外的任何字符</p>
<p><code>[abc]</code>: 字符组  匹配包含括号内元素的字符</p>
<p><code>\W</code>：匹配任意不是字母，数字，下划线 的字符</p>
<p><code>\S</code>：匹配任意不是空白符的字符</p>
<p><code>\D</code>：匹配任意非数字的字符</p>
<p><code>\B</code>：匹配不是单词开头或结束的位置</p>
<p><code>[^abc]</code>：匹配除了abc以外的任意字符</p>
<h2 id="函数-1"><a href="#函数-1" class="headerlink" title="函数"></a>函数</h2><p><code>re.match()</code>：从头开始匹配，匹配的内容必须从头开始，不然不能匹配到</p>
<p><code>re.search()</code>：从字符串中搜索一个能够匹配的出来，这个匹配结果在字符串中的位置没有要求</p>
<p><code>re.findall()</code>:搜索字符串，以列表的形式返回全部字符串</p>
<p><code>re.sub()</code>：替换</p>
<h2 id="原子"><a href="#原子" class="headerlink" title="原子"></a>原子</h2><p>原子是正则表达式中最基本的组成单位，每个正则表达式中至少要包含一个原子，常见的原子类型有：</p>
<ul>
<li>普通字符作为原子</li>
<li>非打印字符作为原子</li>
<li>通用字符作为原子</li>
<li>原子表</li>
</ul>
<h5 id="以普通字符作为原子"><a href="#以普通字符作为原子" class="headerlink" title="以普通字符作为原子"></a>以普通字符作为原子</h5><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">pattern = <span class="string">'baidu'</span></span><br><span class="line">string = <span class="string">'http://www.baidu.com'</span></span><br><span class="line"><span class="comment"># 使用search方法进行匹配，第一个参数是正则表达式，第二个参数是字符串</span></span><br><span class="line">result = re.search(pattern, string)</span><br><span class="line">print(result)</span><br></pre></td></tr></table></figure>
<h5 id="group-方法返回匹配结果"><a href="#group-方法返回匹配结果" class="headerlink" title="group()方法返回匹配结果"></a>group()方法返回匹配结果</h5><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">print(result.group())</span><br></pre></td></tr></table></figure>
<h5 id="span-方法获取匹配结果的范围"><a href="#span-方法获取匹配结果的范围" class="headerlink" title="span()方法获取匹配结果的范围"></a>span()方法获取匹配结果的范围</h5><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">print(result.span())</span><br></pre></td></tr></table></figure>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">string = <span class="string">'abcd'</span></span><br><span class="line">pattern = <span class="string">'baidu'</span></span><br><span class="line">result = re.search(pattern, string)</span><br><span class="line">print(result)</span><br></pre></td></tr></table></figure>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'''以非打印字符作为原子'''</span></span><br><span class="line">pattern = <span class="string">'\n'</span></span><br><span class="line">string = <span class="string">'''abc</span></span><br><span class="line"><span class="string">            de'''</span></span><br><span class="line">result = re.search(pattern, string)</span><br><span class="line">print(result)</span><br></pre></td></tr></table></figure>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">pattern = <span class="string">'python\d'</span></span><br><span class="line">string = <span class="string">'asdfgj'</span></span><br><span class="line">result = re.search(pattern, string)</span><br><span class="line">print(result)</span><br></pre></td></tr></table></figure>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">pattern = <span class="string">'python\d'</span></span><br><span class="line">string = <span class="string">'sdhhaskpython3dsa'</span></span><br><span class="line">result = re.search(pattern, string)</span><br><span class="line">print(result)</span><br></pre></td></tr></table></figure>
<h2 id="原子表"><a href="#原子表" class="headerlink" title="原子表"></a>原子表</h2><ul>
<li>用方括号括起来的字符都是平等的，但是只能匹配一个,例如[a,b,c]这个原子表，只会匹配其中一个原子<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">pattern = <span class="string">'[asd]python\d'</span></span><br><span class="line">string = <span class="string">'sdhhaskpython3dsa'</span></span><br><span class="line">result = re.search(pattern, string)</span><br><span class="line">print(result)</span><br></pre></td></tr></table></figure>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 修改字符串</span></span><br><span class="line">pattern = <span class="string">'[asd]python\d'</span></span><br><span class="line">string = <span class="string">'sdhhasapython3dsa'</span></span><br><span class="line">result = re.search(pattern, string)</span><br><span class="line">print(result)</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h2 id="元字符"><a href="#元字符" class="headerlink" title="元字符"></a>元字符</h2><h3 id="元字符就是正则表达式中具有一些特殊含义的字符，比如重复N次前面的字符"><a href="#元字符就是正则表达式中具有一些特殊含义的字符，比如重复N次前面的字符" class="headerlink" title="元字符就是正则表达式中具有一些特殊含义的字符，比如重复N次前面的字符"></a>元字符就是正则表达式中具有一些特殊含义的字符，比如重复N次前面的字符</h3><p><code>.</code>：匹配任意字符</p>
<p><code>^</code>：匹配字符串开始的位置</p>
<p><code>$</code>：匹配字符串结束的位置</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">pattern = <span class="string">'.python..'</span></span><br><span class="line">string = <span class="string">'hkjhpythonsxkaj'</span></span><br><span class="line">result = re.search(pattern, string)</span><br><span class="line">print(result)</span><br></pre></td></tr></table></figure>
<h2 id="模式分组"><a href="#模式分组" class="headerlink" title="模式分组"></a>模式分组</h2><p><code>()</code>：通常用于提取某一个内容上，使用正则表达式匹配的时候需要将里面的某些内容提取出来的时候就将特定的表达式内容放到小括号中去</p>
<p><code>|</code>：或，只匹配在字符串中出现的第一个</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">pattern = <span class="string">'python|java'</span></span><br><span class="line">string = <span class="string">'hjavadhaiupythonsdjk'</span></span><br><span class="line">result = re.search(pattern, string)</span><br><span class="line">print(result.group())</span><br></pre></td></tr></table></figure>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># group(1)取出第一个匹配目标中的结果</span></span><br><span class="line">pattern = <span class="string">'python(\d+)(.)e'</span></span><br><span class="line">string = <span class="string">'python123456789re'</span></span><br><span class="line">result = re.search(pattern, string)</span><br><span class="line">print(result.group(<span class="number">1</span>))</span><br><span class="line">print(result.group(<span class="number">2</span>))</span><br></pre></td></tr></table></figure>
<h2 id="模式修正符"><a href="#模式修正符" class="headerlink" title="模式修正符"></a>模式修正符</h2><p><code>I</code>：让表达式忽略大小写</p>
<p><code>M</code>：多行匹配</p>
<p><code>L</code>：本地化识别匹配</p>
<p><code>U</code>：根据Unicode字符解析字符</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">pattern = <span class="string">'python'</span></span><br><span class="line">string = <span class="string">'dshdjkPythonfsid'</span></span><br><span class="line">result = re.search(pattern,string)</span><br><span class="line">print(result)</span><br><span class="line"><span class="comment"># 应为严格区分大小写，所以不能匹配出来</span></span><br></pre></td></tr></table></figure>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'''用模式修正符I，匹配的时候忽略大小写'''</span></span><br><span class="line">pattern = <span class="string">'python'</span></span><br><span class="line">string = <span class="string">'dshdjkPythonfsid'</span></span><br><span class="line">result = re.search(pattern, string, re.I)</span><br><span class="line">print(result.group())</span><br></pre></td></tr></table></figure>

<p> <code>S</code>：让<code>.</code>匹配的内容也包括换行符</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">pattern = <span class="string">'abc.+'</span></span><br><span class="line">string = <span class="string">'''abc</span></span><br><span class="line"><span class="string">            de'''</span></span><br><span class="line">result = re.search(pattern, string)</span><br><span class="line">print(result)</span><br></pre></td></tr></table></figure>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">pattern = <span class="string">'abc.'</span></span><br><span class="line">string = <span class="string">'''abc</span></span><br><span class="line"><span class="string">            de'''</span></span><br><span class="line">result = re.search(pattern, string, re.S)</span><br><span class="line">print(result)</span><br></pre></td></tr></table></figure>
<h2 id="量词"><a href="#量词" class="headerlink" title="量词"></a>量词</h2><p><code>*</code>：重复零次或者更多次（贪婪）例如”aaaaaaaa” 匹配字符串中所有的a  正则： “a*”   会出到<code>所有的字符&quot;a&quot;</code></p>
<p><code>+</code>：重复一次或者更多次（懒惰）例如”aaaaaaaa” 匹配字符串中所有的a  正则： “a+”  会取到字符中所有的a字符，  “a+”与”a*”不同在于”+”至少是一次而”*” 可以是0次</p>
<p><code>?</code>：重复零次或者一次（占有）例如”aaaaaaaa” 匹配字符串中的a 正则 ： “a?” 只会匹配一次，也就是结果只是单个字符a</p>
<p><code>{n}</code>：重复n次  例如从”aaaaaaaa” 匹配字符串的a 并重复3次 正则：  “a{3}”  结果就是取到3个a字符  “aaa”</p>
<p><code>{n,m}</code>：重复n到m次  例如正则 “a{3,4}” 将a重复匹配3次或者4次 所以供匹配的字符可以是三个”aaa”也可以是四个”aaaa” 正则都可以匹配到</p>
<p><code>{n,}</code>：重复n次或更多次  与{n,m}不同之处就在于匹配的次数将没有上限，但至少要重复n次 如 正则”a{3,}” a至少要重复3次</p>
<h2 id="懒惰限定符"><a href="#懒惰限定符" class="headerlink" title="懒惰限定符"></a>懒惰限定符</h2><p><code>*?</code>：重复任意次，但尽可能少重复 如 “acbacb”  正则<code>&quot;a.*?b&quot;</code>只会取到第一个”acb” 原本可以全部取到但加了限定符后，只会匹配尽可能少的字符 ，而”acbacb”最少字符的结果就是”acb” </p>
<p><code>+?</code>：重复1次或更多次，但尽可能少重复，与<code>*?</code>一样，只是至少要重复1次</p>
<p><code>??</code>：重复0次或1次，但尽可能少重复，如 “aaacb” 正则 “a.??b” 只会取到最后的三个字符”acb”</p>
<p><code>{n,m}?</code>：重复n到m次，但尽可能少重复，如 “aaaaaaaa”  正则 “a{0,m}” 因为最少是0次所以取到结果为空</p>
<p><code>{n,}?</code>：重复n次以上，但尽可能少重复，如 “aaaaaaa”  正则 “a{1,}” 最少是1次所以取到结果为 “a”</p>
<h2 id="贪婪模式与懒惰模式"><a href="#贪婪模式与懒惰模式" class="headerlink" title="贪婪模式与懒惰模式"></a>贪婪模式与懒惰模式</h2><p>贪婪模式就是经可能多的去匹配，懒惰模式就是尽可能少的匹配</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'''贪婪模式'''</span></span><br><span class="line">pattern = <span class="string">'python.*(\d+)re'</span></span><br><span class="line">string = <span class="string">'python123456789re'</span></span><br><span class="line">result = re.search(pattern,string)</span><br><span class="line">print(result.group(<span class="number">1</span>))</span><br></pre></td></tr></table></figure>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'''懒惰模式'''</span></span><br><span class="line">pattern = <span class="string">'python.*?(\d+)re'</span></span><br><span class="line">string = <span class="string">'python123456789re'</span></span><br><span class="line">result = re.search(pattern,string)</span><br><span class="line">print(result.group(<span class="number">1</span>))</span><br></pre></td></tr></table></figure>

<h2 id="转义字符-1"><a href="#转义字符-1" class="headerlink" title="转义字符"></a>转义字符</h2><ul>
<li>想要匹配特殊字符，可以在特殊字符前加一个换行符\<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">string = <span class="string">'price is 10.50$'</span></span><br><span class="line">pattern = <span class="string">'price is (\d&#123;2&#125;.\d&#123;2&#125;)$'</span></span><br><span class="line">result = re.match(pattern, string)</span><br><span class="line">print(result.group())</span><br></pre></td></tr></table></figure>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">string = <span class="string">'price is 10.50$'</span></span><br><span class="line">pattern = <span class="string">'price is (\d&#123;2&#125;\.\d&#123;2&#125;)\$'</span></span><br><span class="line">result = re.match(pattern, string)</span><br><span class="line">print(result.group(<span class="number">1</span>)</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h2 id="re-sub"><a href="#re-sub" class="headerlink" title="re.sub()"></a>re.sub()</h2><p>re.sub()需要三个参数，第一个参数是正则表达式，第二个参数是拿去替换的字符串，第三个是需要替换的字符串</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">string = <span class="string">"&#123;'a'= 'A', 'b'='B'&#125;"</span></span><br><span class="line">pattern = <span class="string">'='</span></span><br><span class="line">result = re.sub(pattern, <span class="string">':'</span>, string)</span><br><span class="line">print(result)</span><br></pre></td></tr></table></figure>




<h1 id="jupyter-使用"><a href="#jupyter-使用" class="headerlink" title="jupyter 使用"></a>jupyter 使用</h1><ul>
<li>命令模式，按Enter切换为编辑模式</li>
<li>编辑模式，按Esc切换为命令模式<h2 id="命令模式"><a href="#命令模式" class="headerlink" title="命令模式"></a>命令模式</h2></li>
<li>H：显示快捷键帮助</li>
<li>F：查找和替换</li>
<li>P：打开命令面板</li>
<li>Ctrl-Enter：运行当前cell</li>
<li>Shift-Enter：运行当前cell并跳转到下一cell</li>
<li>Alt-Enter：运行当前cell并在下方新建cell</li>
<li>Y：把当前cell内容转换为代码形式</li>
<li>M：把当前cell内容转换为markdown形式</li>
<li>1<del>6：把当前cell内容设置为标题1</del>6格式</li>
<li>Shift+上下键：按住Shift进行上下键操作可复选多个cell</li>
<li>A：在上方新建cell</li>
<li>B：在下方新建cell</li>
<li>X/C/Shift-V/V：剪切/复制/上方粘贴/下方粘贴</li>
<li>双击D：删除当前cell</li>
<li>Z：撤销删除</li>
<li>S：保存notebook</li>
<li>L：为当前cell的代码添加行编号</li>
<li>Shift-L：为所有cell的代码添加行编号</li>
<li>Shift-M：合并所选cell或合并当前cell和下方的cell</li>
<li>双击I：停止kernel</li>
<li>双击0：重启kernel</li>
</ul>
<h2 id="编辑模式"><a href="#编辑模式" class="headerlink" title="编辑模式"></a>编辑模式</h2><ul>
<li>ab：代码补全</li>
<li>Ctrl-A：全选</li>
<li>Ctrl-Z：撤销</li>
<li>Ctrl-Home：将光标移至cell最前端</li>
<li>Ctrl-End：将光标移至cell末端</li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/04/24/hello-world/" rel="prev" title="Hello World">
      <i class="fa fa-chevron-left"></i> Hello World
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/04/30/%E7%9C%81%E4%BB%BD%E8%B6%85%E9%93%BE%E6%8E%A5/" rel="next" title="省份超链接">
      省份超链接 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#基础语法"><span class="nav-number">1.</span> <span class="nav-text">基础语法</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#variable"><span class="nav-number">1.1.</span> <span class="nav-text">variable</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#tuple"><span class="nav-number">1.2.</span> <span class="nav-text">tuple</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#切片"><span class="nav-number">1.2.0.1.</span> <span class="nav-text">切片</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#元组拆包"><span class="nav-number">1.2.0.2.</span> <span class="nav-text">元组拆包</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#注意"><span class="nav-number">1.2.0.3.</span> <span class="nav-text">注意</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#transform"><span class="nav-number">1.3.</span> <span class="nav-text">transform</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#int"><span class="nav-number">1.3.0.1.</span> <span class="nav-text">int</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#float"><span class="nav-number">1.3.0.2.</span> <span class="nav-text">float</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#str"><span class="nav-number">1.3.0.3.</span> <span class="nav-text">str</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#list"><span class="nav-number">1.3.0.4.</span> <span class="nav-text">list</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#string"><span class="nav-number">1.4.</span> <span class="nav-text">string</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#转义字符"><span class="nav-number">1.4.0.1.</span> <span class="nav-text">转义字符</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#三重引号"><span class="nav-number">1.4.0.2.</span> <span class="nav-text">三重引号</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#获取字符长度"><span class="nav-number">1.4.0.3.</span> <span class="nav-text">获取字符长度</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#strip-删除开头或结尾空白字符"><span class="nav-number">1.4.0.4.</span> <span class="nav-text">strip()删除开头或结尾空白字符</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#count-获取某个字符在字符串出现的次数"><span class="nav-number">1.4.0.5.</span> <span class="nav-text">count()获取某个字符在字符串出现的次数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#replace-oldstr-newstr-字符串替换"><span class="nav-number">1.4.0.6.</span> <span class="nav-text">replace(oldstr,newstr)字符串替换</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#format-字符串格式化"><span class="nav-number">1.4.0.7.</span> <span class="nav-text">format()字符串格式化</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#split-字符串的分割"><span class="nav-number">1.4.0.8.</span> <span class="nav-text">split()字符串的分割</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#join-连接字符串"><span class="nav-number">1.4.0.9.</span> <span class="nav-text">join()连接字符串</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#字符串相加，字符串相乘"><span class="nav-number">1.4.0.10.</span> <span class="nav-text">字符串相加，字符串相乘</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#字符串下标"><span class="nav-number">1.4.0.11.</span> <span class="nav-text">字符串下标</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#字符串切片"><span class="nav-number">1.4.0.12.</span> <span class="nav-text">字符串切片</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#set"><span class="nav-number">1.5.</span> <span class="nav-text">set</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#集合运算"><span class="nav-number">1.5.0.1.</span> <span class="nav-text">集合运算</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#添加元素"><span class="nav-number">1.5.0.1.1.</span> <span class="nav-text">添加元素</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#移除指定元素"><span class="nav-number">1.5.0.1.2.</span> <span class="nav-text">移除指定元素</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#pop"><span class="nav-number">1.5.0.1.3.</span> <span class="nav-text">pop</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#difference"><span class="nav-number">1.5.0.1.4.</span> <span class="nav-text">difference</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#difference-update"><span class="nav-number">1.5.0.1.5.</span> <span class="nav-text">difference_update</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#in-not-in"><span class="nav-number">1.5.0.1.6.</span> <span class="nav-text">in&#x2F;not in</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#去重"><span class="nav-number">1.5.0.1.7.</span> <span class="nav-text">去重</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#列表"><span class="nav-number">1.6.</span> <span class="nav-text">列表</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#创建列表"><span class="nav-number">1.6.0.1.</span> <span class="nav-text">创建列表</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#乘法"><span class="nav-number">1.6.0.2.</span> <span class="nav-text">乘法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#加法"><span class="nav-number">1.6.0.3.</span> <span class="nav-text">加法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#索引"><span class="nav-number">1.6.0.4.</span> <span class="nav-text">索引</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#切片-1"><span class="nav-number">1.6.0.5.</span> <span class="nav-text">切片</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#append"><span class="nav-number">1.6.0.6.</span> <span class="nav-text">append</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#extend"><span class="nav-number">1.6.0.7.</span> <span class="nav-text">extend</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#insert"><span class="nav-number">1.6.0.8.</span> <span class="nav-text">insert</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#remove"><span class="nav-number">1.6.0.9.</span> <span class="nav-text">remove</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#del"><span class="nav-number">1.6.0.10.</span> <span class="nav-text">del</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#max-min"><span class="nav-number">1.6.0.11.</span> <span class="nav-text">max&#x2F;min</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#嵌套列表"><span class="nav-number">1.6.0.12.</span> <span class="nav-text">嵌套列表</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#在Python中一般不用列表来表示二维数组或者多维数组，虽然Python也能表示，但是性能较低，一般使用array或者-numpy来创建多位数组"><span class="nav-number">1.7.</span> <span class="nav-text">在Python中一般不用列表来表示二维数组或者多维数组，虽然Python也能表示，但是性能较低，一般使用array或者 numpy来创建多位数组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#整型（int）"><span class="nav-number">1.8.</span> <span class="nav-text">整型（int）</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#整型不能用-len-函数"><span class="nav-number">1.8.1.</span> <span class="nav-text">整型不能用 len() 函数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#if"><span class="nav-number">1.9.</span> <span class="nav-text">if</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#if-else-语句"><span class="nav-number">1.9.1.</span> <span class="nav-text">if else 语句</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#if-elif-else"><span class="nav-number">1.9.2.</span> <span class="nav-text">if elif else</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#pass"><span class="nav-number">1.9.3.</span> <span class="nav-text">pass</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#函数"><span class="nav-number">1.10.</span> <span class="nav-text">函数</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#caution：函数没有调用的时候，这段代码不会执行"><span class="nav-number">1.10.0.0.1.</span> <span class="nav-text">caution：函数没有调用的时候，这段代码不会执行</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#函数参数"><span class="nav-number">1.10.0.1.</span> <span class="nav-text">函数参数</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#必备参数"><span class="nav-number">1.10.0.1.1.</span> <span class="nav-text">必备参数</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#默认参数"><span class="nav-number">1.10.0.1.2.</span> <span class="nav-text">默认参数</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#不定长参数"><span class="nav-number">1.10.0.1.3.</span> <span class="nav-text">不定长参数</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#匿名参数"><span class="nav-number">1.10.0.2.</span> <span class="nav-text">匿名参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#return"><span class="nav-number">1.10.0.3.</span> <span class="nav-text">return</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#全局变量和局部变量"><span class="nav-number">1.10.0.4.</span> <span class="nav-text">全局变量和局部变量</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#global"><span class="nav-number">1.10.0.5.</span> <span class="nav-text">global</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#递归函数"><span class="nav-number">1.10.0.6.</span> <span class="nav-text">递归函数</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#浮点数-float"><span class="nav-number">1.11.</span> <span class="nav-text">浮点数(float)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#文件操作"><span class="nav-number">1.11.1.</span> <span class="nav-text">文件操作</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#open函数负责打开文件，并且返回文件对象"><span class="nav-number">1.11.1.0.1.</span> <span class="nav-text">open函数负责打开文件，并且返回文件对象</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#read-write-close三个方法都需要调用文件对象来调用"><span class="nav-number">1.11.1.0.2.</span> <span class="nav-text">read&#x2F;write&#x2F;close三个方法都需要调用文件对象来调用</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#read方法读取文件"><span class="nav-number">1.11.1.1.</span> <span class="nav-text">read方法读取文件</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#打开文件的方式"><span class="nav-number">1.11.1.1.1.</span> <span class="nav-text">打开文件的方式</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#执行读取文件内容"><span class="nav-number">1.11.1.2.</span> <span class="nav-text">执行读取文件内容</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#read方法默认把文件的所有内容一次性读取到内存中，如果文件过大，内存占用会非常严重"><span class="nav-number">1.11.1.2.1.</span> <span class="nav-text">read方法默认把文件的所有内容一次性读取到内存中，如果文件过大，内存占用会非常严重</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#readline方法可以一次读取一行内容-方法执行之后，会把文件指针移动到下一行，准备再次读取"><span class="nav-number">1.11.1.2.2.</span> <span class="nav-text">readline方法可以一次读取一行内容 方法执行之后，会把文件指针移动到下一行，准备再次读取</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#文件读写复制"><span class="nav-number">1.11.2.</span> <span class="nav-text">文件读写复制</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#小文件"><span class="nav-number">1.11.2.1.</span> <span class="nav-text">小文件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#大文件"><span class="nav-number">1.11.2.2.</span> <span class="nav-text">大文件</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#with-open"><span class="nav-number">1.11.3.</span> <span class="nav-text">with open</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#error"><span class="nav-number">1.12.</span> <span class="nav-text">error</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#异常的捕获"><span class="nav-number">1.12.0.1.</span> <span class="nav-text">异常的捕获</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#异常响应"><span class="nav-number">1.12.0.2.</span> <span class="nav-text">异常响应</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#异常的传递"><span class="nav-number">1.12.0.3.</span> <span class="nav-text">异常的传递</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#抛出异常"><span class="nav-number">1.12.0.4.</span> <span class="nav-text">抛出异常</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#示例"><span class="nav-number">1.12.0.4.1.</span> <span class="nav-text">示例</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#字典"><span class="nav-number">1.13.</span> <span class="nav-text">字典</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#删除"><span class="nav-number">1.13.1.</span> <span class="nav-text">删除</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#get"><span class="nav-number">1.13.2.</span> <span class="nav-text">get</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#update"><span class="nav-number">1.13.3.</span> <span class="nav-text">update</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cycle"><span class="nav-number">1.13.4.</span> <span class="nav-text">cycle</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#正则表达式"><span class="nav-number">2.</span> <span class="nav-text">正则表达式</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#常见匹配模式"><span class="nav-number">2.1.</span> <span class="nav-text">常见匹配模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#函数-1"><span class="nav-number">2.2.</span> <span class="nav-text">函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#原子"><span class="nav-number">2.3.</span> <span class="nav-text">原子</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#以普通字符作为原子"><span class="nav-number">2.3.0.0.1.</span> <span class="nav-text">以普通字符作为原子</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#group-方法返回匹配结果"><span class="nav-number">2.3.0.0.2.</span> <span class="nav-text">group()方法返回匹配结果</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#span-方法获取匹配结果的范围"><span class="nav-number">2.3.0.0.3.</span> <span class="nav-text">span()方法获取匹配结果的范围</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#原子表"><span class="nav-number">2.4.</span> <span class="nav-text">原子表</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#元字符"><span class="nav-number">2.5.</span> <span class="nav-text">元字符</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#元字符就是正则表达式中具有一些特殊含义的字符，比如重复N次前面的字符"><span class="nav-number">2.5.1.</span> <span class="nav-text">元字符就是正则表达式中具有一些特殊含义的字符，比如重复N次前面的字符</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#模式分组"><span class="nav-number">2.6.</span> <span class="nav-text">模式分组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#模式修正符"><span class="nav-number">2.7.</span> <span class="nav-text">模式修正符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#量词"><span class="nav-number">2.8.</span> <span class="nav-text">量词</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#懒惰限定符"><span class="nav-number">2.9.</span> <span class="nav-text">懒惰限定符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#贪婪模式与懒惰模式"><span class="nav-number">2.10.</span> <span class="nav-text">贪婪模式与懒惰模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#转义字符-1"><span class="nav-number">2.11.</span> <span class="nav-text">转义字符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#re-sub"><span class="nav-number">2.12.</span> <span class="nav-text">re.sub()</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#jupyter-使用"><span class="nav-number">3.</span> <span class="nav-text">jupyter 使用</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#命令模式"><span class="nav-number">3.1.</span> <span class="nav-text">命令模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#编辑模式"><span class="nav-number">3.2.</span> <span class="nav-text">编辑模式</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="樱桃小睿"
      src="/images/QQ.jpg">
  <p class="site-author-name" itemprop="name">樱桃小睿</p>
  <div class="site-description" itemprop="description">可甜可御pick me  up</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">10</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">樱桃小睿</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v4.2.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> v7.7.1
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
